package com.ce.pms.charge.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ce.pms.base.service.impl.PmsBaseServiceImpl;
import com.ce.pms.charge.dao.IBankAccountDao;
import com.ce.pms.charge.dao.IFeeBankDao;
import com.ce.pms.charge.model.BankDomain;
import com.ce.pms.charge.service.IFeeBankService;
import com.ce.pms.charge.vo.BankVO;
import com.ce.pms.common.model.SysInfo;
import com.ce.pms.exception.BusinessException;
import com.ce.util.BlankUtil;
import com.sitechasia.webx.core.support.Page;
import com.sitechasia.webx.core.utils.base.PopulateUtil;
/**
 * 功能描述:划款银行service实现类
 * <p>版权所有：中企动力
 * <p>未经本公司许可，不得以任何方式复制或使用本程序任何部分
 *
 * @author  胡煦
 * Create on:2008-7-30
 * Edit author:
 * Edit on:
 * 修改原因：

 */

public class FeeBankServiceImpl extends PmsBaseServiceImpl implements
		IFeeBankService {

	/**
	 * 方法描述: 根据银行ＩＤ判断其是否被引用
	 * @author 胡煦
	 * Create on:2008-9-5
	 * @param bankId
	 * @return boolean:返回true说明未被引用，返回false则说明已经被引用
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public boolean checkBankIsUsed(Long bankId){
		return bankAccountDao.getBankAccountCountByBankId(bankId) == 0;
	}

	/**
	 * 功能描述:检查回盘文件名的前缀和指定银行的前缀是否一致<br>
	 * @param bankId
	 * @param inPreFixFileName
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2008-10-7<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	public boolean checkInPreFix(Long bankId,String inPreFixFileName){
		BankDomain bankDomain= feeBankDao.findById(bankId);
		String inPreFix=inPreFixFileName.split("-")[0];

		return inPreFix.equals(bankDomain.getOutFilePrefix());
	}

	/**
	 * 方法描述: 分页查询所有划款银行
	 *
	 * @author 胡煦 Create on:2008-7-30
	 * @param nameStr：
	 * @return 返回当前页的查询结果 Edit author: Edit on: 修改原因：
	 */
	public Page queryAllFeeBankByPage(int pageNo, int pageSize,String order){

		order = order == null || "".equals(order) ? "asc" : order;
		Page page = feeBankDao.queryAllFeeBankByPage(pageNo, pageSize,order);

		List<BankDomain> bankDomainList = new ArrayList<BankDomain>();
		List<BankVO> bankVOList = new ArrayList<BankVO>();
		bankDomainList = page.getResult();
		for(BankDomain bankDomain : bankDomainList){
			BankVO bankVO = this.convertDoToVo(bankDomain);
			bankVOList.add(bankVO);
		}
		page.setResult(bankVOList);
		return page;
	}

	/**
	 * 功能描述: 得到所有银行列表 <br>
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2008-8-26  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public List<BankVO> getAllBankList(){
		List<BankDomain> list = this.feeBankDao.findAll();

		List<BankVO> resList = new ArrayList<BankVO>();
		if (!BlankUtil.isBlank(list)) {
			for (BankDomain domain : list) {
				resList.add(this.convertDoToVo(domain));
			}
		}
		return resList;
	}
	/**
	 * 方法描述: 增加划款银行
	 * @author 胡煦
	 * Create on:2008-7-30
	 * @param nameStr：BankVO
	 * @return BankVO
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public BankVO addFeeBank(BankVO bankVO){

		checkNotNullField(bankVO,"common:mustFill_null_exception");
		checkBankIsOnly(bankVO);
		BankDomain bankDomain = feeBankDao.saveOrUpdateObject(this.convertVoToDo(bankVO));
		bankVO.setKeyword(bankDomain.getKeyword());
		return bankVO;
	}
	/**
	 * 方法描述: 修改划款银行
	 * @author 胡煦
	 * Create on:2008-7-30
	 * @param nameStr：BankVO
	 * @return BankVO
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public BankVO editFeeBank(BankVO bankVO){
		checkNotNullField(bankVO,"common:mustFill_null_exception");
		checkBankIsOnly(bankVO);
		feeBankDao.saveOrUpdateObject(this.convertVoToDo(bankVO));
		return bankVO;
	}
	/**
	 * 方法描述: 删除划款银行
	 * @author 胡煦
	 * Create on:2008-7-30
	 * @param nameStr：bankId
	 * @return void
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public void deleteFeeBankById(Long bankId){
		super.checkParameterIsNull(bankId, "common:parameters_null_exception,ID");
		try {
			feeBankDao.deleteObjectById(bankId);
		} catch (RuntimeException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 功能描述:  根据划款银行ID获取划款银行VO<br>
	 * @param bankId
	 * Create author:yixianjian <br>
	 * Create on:2008-8-5  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public BankVO getFeeBankById(Long bankId) {
		super.checkParameterIsNull(bankId, "common:parameters_null_exception,ID");
		BankDomain bankDomain = feeBankDao.findById(bankId);
		return this.convertDoToVo(bankDomain);
	}
	/**
	 * 方法描述: 检查划款银行名称是否唯一
	 *  注：新建判断bankName的唯一性时，bankId为null；修改判断bankName的唯一性时，则需要传入修改前的bankId<br>
	 * @author 胡煦
	 * Create on:2008-7-30
	 * @param bankName
	 * @return boolean
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public boolean checkBankNameIsOnly(String bankName,Long bankId){
		super.checkParameterIsNull(bankName, "common:parameters_null_exception,seqNo");
		return feeBankDao.checkBankNameIsOnly(bankName, bankId) != 0;
	}
	/**
	 * 方法描述: 检查划款银行代码是否唯一
	 *  注：新建判断Code的唯一性时，bankId为null；修改判断Code的唯一性时，则需要传入修改前的bankId<br>
	 * @author 胡煦
	 * Create on:2008-7-30
	 * @param bankCode
	 * @return boolean
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public boolean checkBankCodeIsOnly(String bankCode,Long bankId){
		super.checkParameterIsNull(bankCode, "common:parameters_null_exception,seqNo");
		return feeBankDao.checkBankCodeIsOnly(bankCode, bankId) != 0;
	}
	/**
	 * 方法描述:  检查划款银行银联代码是否唯一
	 *  注：新建判断Code的唯一性时，bankId为null；修改判断Code的唯一性时，则需要传入修改前的bankId<br>
	 * @author 胡煦
	 * Create on:2008-7-30
	 * @param uniteCode
	 * @return boolean
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public boolean checkUniteCodeIsOnly(String uniteCode,Long bankId){
		return false;
	}
	/**
	 * 方法描述: 检查划款银行出盘文件前缀名是否唯一
	 *  注：新建判断OFilePrefix的唯一性时，bankId为null；修改判断OFilePrefix的唯一性时，则需要传入修改前的bankId<br>
	 * @author 胡煦
	 * Create on:2008-7-30
	 * @param oFilePrefix
	 * @return boolean
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public boolean checkoFilePrefixIsOnly(String oFilePrefix, Long bankId) {
		super.checkParameterIsNull(oFilePrefix, "common:parameters_null_exception,seqNo");
		return feeBankDao.checkoFilePrefixIsOnly(oFilePrefix, bankId) != 0;
	}
	/**
	 * 方法描述: 检查划款银行中各项是否唯一
	 * @author 胡煦
	 * Create on:2008-7-30
	 * @param bankName
	 * @return boolean
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public void checkBankIsOnly(BankVO bankVO){
		super.checkParameterIsNull(bankVO, "common:parameters_null_exception");

		if(this.checkBankNameIsOnly(bankVO.getBankName(), bankVO.getKeyword())){
			throw new BusinessException("bank_name_exist_exception",BusinessException.WARNING);
		}
		if(this.checkBankCodeIsOnly(bankVO.getBankCode(), bankVO.getKeyword())){
			throw new BusinessException("bank_Code_exist_exception",BusinessException.WARNING);
		}
		if(this.checkoFilePrefixIsOnly(bankVO.getOutFilePrefix(), bankVO.getKeyword())){
			throw new BusinessException("bank_oFilePrefix_exist_exception",BusinessException.WARNING);
		}
	}
	/**
	 * 方法描述: BankDomain to BankVO
	 * @author 胡煦
	 * Create on:2008-7-31
	 * @param BankDomain
	 * @return BankVO
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	private BankVO convertDoToVo(BankDomain bankDomain){
		BankVO bankVO = new BankVO();
		if(BlankUtil.isBlank(bankDomain)){
			return bankVO;
		}
		PopulateUtil.populate(bankDomain, bankVO);
		bankVO.setCreator(bankDomain.getSysInfo()==null?null:bankDomain.getSysInfo().getCreator());
		bankVO.setModifier(bankDomain.getSysInfo()==null?null:bankDomain.getSysInfo().getModifier());
		bankVO.setModifyDate(bankDomain.getSysInfo()==null?null:bankDomain.getSysInfo().getModifyDate());
		bankVO.setSysDate(bankDomain.getSysInfo()==null?null:bankDomain.getSysInfo().getSysDate());
		return bankVO;
	}
	/**
	 * 方法描述:  BankVO to BankDomain
	 * @author 胡煦
	 * Create on:2008-7-31
	 * @param BankVO
	 * @return BankDomain
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	private BankDomain convertVoToDo(BankVO bankVO){
		BankDomain bankDomain = new BankDomain();
		if(BlankUtil.isBlank(bankVO)){
			return bankDomain;
		}
		PopulateUtil.populate(bankVO, bankDomain);
		//处理出盘、回盘前缀
		String filePrefix = bankVO.getOutFilePrefix();
		bankDomain.setInFilePrefix("IN_" + filePrefix);
		bankDomain.setOutFilePrefix("OUT_" + filePrefix);

		SysInfo sysInfo = createInfo(bankVO.getCreator());
		if(BlankUtil.isBlank(bankVO.getModifier())){
			modifyInfo(sysInfo, bankVO.getModifier());
		}
		bankDomain.setSysInfo(sysInfo);

		return bankDomain;
	}
	/**
	 * 方法描述: 检查划款银行中必填项是否为空
	 * @author 胡煦
	 * Create on:2008-7-30
	 * @param bankName
	 * @return boolean
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	private void checkNotNullField(BankVO bankVO,String key){
		if(bankVO == null || bankVO.getBankCode() == null||bankVO.getBankName() == null ||bankVO.getOutFilePrefix() == null){
			throw new BusinessException(key,BusinessException.WARNING);
		}
	}

	private IFeeBankDao feeBankDao;
	private IBankAccountDao bankAccountDao;

	/**
	 * @param feeBankDao the feeBankDao to set
	 */
	public void setFeeBankDao(IFeeBankDao feeBankDao) {
		this.feeBankDao = feeBankDao;
	}


	/**
	 * @param bankAccountDao the bankAccountDao to set
	 */
	public void setBankAccountDao(IBankAccountDao bankAccountDao) {
		this.bankAccountDao = bankAccountDao;
	}
}
